define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'summary/index' + location.search,
                    import_url: 'summary/import',
                    table: 'summary',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='admin_id']", form).addClass("selectpage").data("source", "auth/admin/index").data("primaryKey", "id").data("field", "nickname").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
         
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'mobile_serial',
                sortOrder: 'asc',
                searchFormVisible: true,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'admin_id', title: __('负责人'), formatter: Table.api.formatter.search},
                        // {field: 'nickname', title: __('负责人'), formatter: Table.api.formatter.search},
                        // {field: 'account.is_open', title: __('是否开好橱窗'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.normal, operate: false},
                        // {field: 'is_open', title: __('是否开好橱窗'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.normal},
                        {field: 'date', title: __('日期'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'mobile_serial', title: __('手机编号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.search},
                        {field: 'volume', title: __('发布总量'), operate: false},
                        {field: 'play_num', title: __('播放总量'), operate: false},
                        {field: 'fans', title: __('总粉丝数'), operate: false},
                        {
                            field: 'fans_diff', 
                            title: __('粉丝差值 '), 
                            operate: false,
                            formatter: function (value, row, index) {
                                if(row.fans > row.yestoday_fans){
                                    return '<span style="color:red;"><i class="fa fa-long-arrow-up"></i> '+row.fans_diff+'</span>';
                                }else if(row.fans < row.yestoday_fans){
                                    return '<span style="color:green;"><i class="fa fa-long-arrow-down"></i> '+row.fans_diff+'</span>';
                                }
                                return value;
                            }
                        },
                        {field: 'yestoday_fans', title: __('昨日粉丝量'), operate: false},
                    ]
                ],
                
                responseHandler: function (data) {
                    console.log(data);
                    $('.fans').html(data.extend.fans);
                    return data;
                }
            });

            $('#date').val(Config.date);
            $(".form-commonsearch").find('button[type=submit]').trigger('click');
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },

        home: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'summary/index' + location.search,
                    import_url: 'summary/import',
                    table: 'summary',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
         
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'mobile_serial',
                sortOrder: 'asc',
                searchFormVisible: true,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'nickname', title: __('负责人'), formatter: Table.api.formatter.search},
                        {field: 'date', title: __('日期'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'mobile_serial', title: __('手机编号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.search},
                        {field: 'play_num', title: __('播放总量'), operate: false},
                        {field: 'volume', title: __('总发布量'), operate: false},
                        {field: 'turnover', title: __('总成交量'), operate: false},
                        {field: 'amount', title: __('总成交佣金'), operate: false},
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.type = 2;
                    op.type = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            $('#date').val(Config.date);
            $(".form-commonsearch").find('button[type=submit]').trigger('click');
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },

        violent: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'summary/index' + location.search,
                    import_url: 'summary/import',
                    table: 'summary',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
         
            // 初始化表格
            table.bootstrapTable({
                url: 'summary/violent' + location.search,
                pk: 'id',
                sortName: 'mobile_serial',
                sortOrder: 'asc',
                searchFormVisible: true,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'nickname', title: __('负责人'), formatter: Table.api.formatter.search},
                        {field: 'date', title: __('日期'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'mobile_serial', title: __('手机编号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.search},
                        {field: 'volume', title: __('总发布量'), operate: false},
                        {field: 'play_num', title: __('播放总量'), operate: false},
                        {field: 'violent_num', title: __('总违规量'), operate: false},
                        {field: 'amount', title: __('总视频佣金'), operate: false},
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.type = 3;
                    op.type = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },

                responseHandler: function (data) {
                    console.log(data);
                    $('.amount').html('￥' + data.extend.amount);
                    return data;
                }
            });

            $('#date').val(Config.date);
            $(".form-commonsearch").find('button[type=submit]').trigger('click');
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            
        }
    };
    return Controller;
});
